package com.bps.zzant.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.bps.zzant.model.ArtFile;
import com.bps.zzant.model.Customer;
import com.bps.zzant.model.OrderGarment;
import com.bps.zzant.model.ToDo;

public class FileDAOImpl implements FileDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void saveFile(ArtFile artFile){
		try {
			
			getSessionFactory().getCurrentSession().save(artFile);
			getSessionFactory().getCurrentSession().flush();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public List<ArtFile> getFileListByOrderId(int customerId, int orderId) {
		
		 // List list = getSessionFactory().getCurrentSession().createQuery("from ToDo todo where todo.owner='"+owner +"' and todo.status='"+status+"'").list();
		
		List list = getSessionFactory().getCurrentSession().createCriteria(ArtFile.class)
				.add(Restrictions.eq("customerId", customerId))
				.add(Restrictions.eq("ordersId", orderId))
				.list();
		
		System.out.println("-----------FileDAOImpl.getFileListByOrderId--- the list size:" + list.size());
		return list;
	}
	
	public void deleteFile(ArtFile artFile) {
		// TODO Auto-generated method stub
		System.out.println("-----------FileDAOImpl.deleteFile--- " + artFile);
		getSessionFactory().getCurrentSession().delete(artFile);
		getSessionFactory().getCurrentSession().flush();

	}
	
	public ArtFile getFileById(int fileId){
		ArtFile artFile = (ArtFile)getSessionFactory().getCurrentSession().get(ArtFile.class, new Integer(fileId));
		return artFile;
	}
}
